<?php
/*
 * File theorie.php for project Rijlesplanner.nl
 * Created on 2 dec 2007 at 21:30:23 by Ivar Pruijn
 */
class theorie extends modules {
	// Properties:
	protected $name = "theorie";
	protected $title = "Rijles theorie oefenen";
	protected $subtitle = "";
	protected $moduleOptions = array ();
	private $errors = array ();

	// Methods:

	/**
	 * void __construct ()
	 * This is the constructor method for this class.
	 */
	public function __construct() {
		parent :: __construct();
		$this->logger = parent :: getLogger();
		$this->user = parent :: getUser();
		$this->template = parent :: getTemplate();
	}

	/**
	 * Object process()
	 * 
	 * Processes the template and current module
	 * 
	 * @param Smarty template The Smarty template to process (sets variables etc.)
	 * @return Smarty The template that has been processed
	 */
	function process(& $template = null) {
		$this->template = $template;
		
		parent::loadModule("ads",$template);
		
		if($this->user instanceof Visitor) {
			parent::loadModule("authentication",$template);
  		} else if($this->user instanceof Leerling) {
  			$this->template->assign('plantijd',((int)$_SESSION['Plantijd']) + 1);
				$this->template->assign('annuleertijd',((int)$_SESSION['Annuleertijd']) + 1);
  			$this->template->assign('rijschool',array('naam'=>$this->user->getRijschoolNaam(),'id'=>$this->user->getRijschoolId()));
  		}
		if (!empty ($_POST)) {
			if (!$this->auth->accessTo($this->name,null,$_POST['action'])) {
				$this->actionAccessDenied();
			} else {
				switch ($_POST['action']) {
	  	    		case "selectCursus":
						if ($this->user instanceof Leerling) {
							$cursusId = $_POST['cursusId'];
							$this->theoriePlannenLeerling($cursusId);
							$this->template->assign('moduleTemplate', 'modules/theorie/theorieplannen_leerling.tpl');
						}
	  	    			break;
	  	    		case "plannen":
						if ($this->user instanceof Leerling) {
							$plannenResult = $this->planTheorieLeerling();
							$this->template->assign('plannenResult', $plannenResult);
							$this->template->assign('moduleTemplate', 'modules/theorie/plantheorie_leerling.tpl');
						}
	  	    			break;
	  	    		case "annuleren":
						if ($this->user instanceof Leerling) {
							$annulerenResult = $this->annuleerTheorieLeerling();
							$this->template->assign('annulerenResult', $annulerenResult);
							$this->template->assign('moduleTemplate', 'modules/theorie/annuleertheorie_leerling.tpl');
						}
	  	    			break;
	  	    		case "theoriedetails":
						if ($this->user instanceof Leerling) {
							$this->theorieDetailsLeerling();
							$this->template->assign('moduleTemplate', 'modules/theorie/theoriedetails_leerling.tpl');
						}
	  	    			break;
	  	    		case "theorieaanvragen":
						if ($this->user instanceof Leerling) {
							
							$aanvraagResult = $this->theorieAanvragenLeerling();
							$this->template->assign('aanvraagResult', $aanvraagResult);
							$this->theorieLerenLeerling();
							$this->template->assign('moduleTemplate', 'modules/theorie/theorieleren_leerling.tpl');
						}
	  	    			break;
				}
			}
		} else if(!empty($_GET['option'])) {
			if (!$this->auth->accessTo($this->name,$_GET['option'])) {
				$this->optionAccessDenied();
			} else {
	  	  		switch ($_GET['option']) {
	  	    		case "waaromrijlesplanner":
						if($this->user instanceof Visitor) {
							parent::loadModule("reviews",$template);
							$this->template->assign('moduleTemplate', 'modules/theorie/waaromrijlesplanner_bezoeker.tpl');
						}
	  	    			break;
	  	    		case "theorieleren":
						if($this->user instanceof Visitor) {
							$this->template->assign('moduleTemplate', 'modules/theorie/theorieleren_bezoeker.tpl');
						} else if ($this->user instanceof Leerling) {
							$this->theorieLerenLeerling();
							$this->template->assign('moduleTemplate', 'modules/theorie/theorieleren_leerling.tpl');
						}
	  	    			break;
	  	    		case "gratistheorie":
						if($this->user instanceof Visitor) {
							$this->template->assign('moduleTemplate', 'modules/theorie/gratistheorie_bezoeker.tpl');
						}
	  	    			break;
	  	    		case "theorierijschool":
						if ($this->user instanceof Leerling) {
							$this->template->assign('moduleTemplate', 'modules/theorie/theorierijschool_leerling.tpl');
						}
	  	    			break;
	  	    		case "theorieplannen":
						if ($this->user instanceof Leerling) {
							$this->theoriePlannenLeerling();
							$this->template->assign('moduleTemplate', 'modules/theorie/theorieplannen_leerling.tpl');
						}
	  	    			break;
	  	    		case "theorieexamens":
						if($this->user instanceof Leerling) {
							$this->template->assign('moduleTemplate', 'modules/theorie/theorieexamens_leerling.tpl');
						}
	  	    			break;
	  	    		default:
	  	    			break;
	  	  		}
			}
		} else {
			parent::loadModule("reviews",$template);
			if($this->user instanceof Visitor) {
				$this->template->assign('moduleTemplate', 'modules/theorie/waaromrijlesplanner_bezoeker.tpl');
			} else if ($this->user instanceof Leerling) {
				$this->theoriePageContent();
				$this->template->assign('moduleTemplate', 'modules/theorie/mijntheorie_leerling.tpl');
			}
		}
	}
	
	function theoriePageContent() {
		$errors = array ();
		try {
			$result = $this->wsclient->WS_Rijlesplanner_GetContent(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'PageId' => 'theorie'
			));
			$content = $result->Content;
			$this->template->assign('pageContent', $content);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
					$errorcode = 62;
					$errormessage = 'Onbekende fout bij ophalen theorie pagina content ("'.$e->faultstring.'")';
					break;
			}
			$errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
		$this->template->assign('errors', $errors);
	}

	/**
	 * void theoriePlannenLeerling()
	 * 
	 * Shows the 'Theorie plannen' page (for a Leerling that is logged in)
	 * Sets an error array in the template in case of errors.
	 */
	function theoriePlannenLeerling($cursusId = 0) {
		$this->errors = array ();
		try {
			$result = $this->wsclient->WS_Leerling_GetCursussen(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'LeerlingId' => $this->user->getId()
			));
			$cursussen = $result->Cursussen;
			$this->template->assign('cursussen',$cursussen);
			
			$cursusNameSelected = "";
			foreach ($cursussen AS $cursus) {
				if ($cursus->getId() == $cursusId) {
					$cursusNameSelected = $cursus->getPakketNaam();
				}
			}
			
			if ($cursusId <= 0 && count($cursussen) > 0) {
				$cursusNameSelected = $cursussen[0]->PakketNaam;
				$cursusId = $cursussen[0]->Id;
			} 
						
			$this->template->assign('cursusNameSelected',$cursusNameSelected);
			$this->template->assign('cursusIdSelected',$cursusId);
			
			// now get the planning for this cursus
			$result = $this->wsclient->WS_AgendaTheorie_Lijst(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'CursusId' => $cursusId,
			));
			$beschikbaar = $result->Beschikbaar;
			$this->template->assign('beschikbaar',$beschikbaar);
			$reedsgepland = $result->ReedsGepland;
			$this->template->assign('reedsgepland',$reedsgepland);
			$this->template->assign('errors', $this->errors);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
					$errorcode = 62;
					$errormessage = 'Onbekende fout bij ophalen theorie pagina content ("'.$e->faultstring.'")';
					break;
			}
			$errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		}
	}
	
	/**
	 * void planTheorieLeerling()
	 * 
	 * Plans a theorie moment (for a Leerling that is logged in)
	 * Sets an error array in the template in case of errors.
	 */
	function planTheorieLeerling() {
		$plannenresult = false;
		$errors = array ();
		try {
			if (!isset ($_POST['cursusId']) || empty ($_POST['cursusId'])) {
				throw new TheoriePlannenException('Geen cursus ID gegeven', 17);
			}
			if (!isset ($_POST['itemId']) || empty ($_POST['itemId'])) {
				throw new TheoriePlannenException('Geen theorie ID gegeven', 18);
			}
			$result = $this->wsclient->WS_AgendaTheorie_Plan(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'CursusId' => $_POST['cursusId'],
				'TheorieId' => $_POST['itemId']
			));
			$plannenresult= $result->Success;
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				case 0 :
					$errorcode = 19;
					$errormessage = 'Theorie item is niet beschikbaar ("'.$e->faultstring.'")';
					break;
				case 1 :
					$errorcode = 20;
					$errormessage = 'Op dit tijdstip mag niet ingepland worden ("'.$e->faultstring.'")';
					break;
				default :
					$errorcode = 72;
					$errormessage = 'Onbekende fout bij theorie plannen ("'.$e->faultstring.'")';
					break;
			}
			$errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (TheoriePlannenException $e) {
			$errors[] = array (
				'code' => $e->getCode(),
				'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('errors', $errors);
		return $plannenresult;
	}				

	
	/**
	 * void annuleerTheorieLeerling()
	 * 
	 * Cancels a theorie moment (for a Leerling that is logged in)
	 * Sets an error array in the template in case of errors.
	 */
	function annuleerTheorieLeerling() {
		$annulerenResult = false;
		$errors = array ();
		try {
			if (!isset ($_POST['cursusId']) || empty ($_POST['cursusId'])) {
				throw new TheorieAnnulerenException('Geen cursus ID gegeven', 17);
			}
			if (!isset ($_POST['itemId']) || empty ($_POST['itemId'])) {
				throw new TheorieAnnulerenException('Geen theorie ID gegeven', 18);
			}
			$result = $this->wsclient->WS_AgendaTheorie_Annuleer(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'CursusId' => $_POST['cursusId'],
				'TheorieId' => $_POST['itemId']
			));
			
			$annulerenResult= $result->Success;
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				case 1 :
					$errorcode = 37;
					$errormessage = 'Op dit tijdstip mag deze theorie niet meer geannuleerd worden';
					break;
				default :
					$errorcode = 16;
					$errormessage = 'Onbekende fout bij theorie annuleren ("'.$e->faultstring.'")';
					break;
			}
			$errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (TheoriePlannenException $e) {
			$errors[] = array (
				'code' => $e->getCode(),
				'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('errors', $errors);
		return $annulerenResult;
	}				

	/**
	 * void theorieLerenLeerling()
	 * 
	 * Shows the 'Theorie leren' page (for a Leerling that is logged in)
	 * Sets an error array in the template in case of errors.
	 */
	function theorieLerenLeerling() {
		$this->errors = array ();
		try {
			
			$result = $this->wsclient->WS_VekaTheorie_ModuleOverzicht(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId']
			));
			
			$this->template->assign('vekamodules',$result->ModuleOverzicht);
			
			$result = $this->wsclient->WS_VekaTheorie_Lijst(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId']
			));
			$theorieen = $result->Lijst;
			$this->template->assign('theorieen',$theorieen);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
					$errorcode = 21;
					$errormessage = 'Onbekende fout bij ophalen theorie lijst ("'.$e->faultstring.'")';
					break;
			}
			$this->errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (TheorieDetailsException $e) {
			$this->errors[] = array (
				'code' => $e->getCode(),
				'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('errors', $this->errors);
	}

	/**
	 * void theorieDetailsLeerling()
	 * 
	 * Shows the 'Theorie details' page (for a Leerling that is logged in)
	 * Sets an error array in the template in case of errors.
	 */
	function theorieDetailsLeerling() {
		$this->errors = array ();
		try {
			if (!isset ($_POST['theorieId']) || empty ($_POST['theorieId'])) {
				throw new TheorieDetailsException('Geen theorie ID gegeven', 22);
			}
			$result = $this->wsclient->WS_VekaTheorie_Details(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'VekaTheorieId' => $_POST['theorieId']
			));
			$this->logger->log($result,PEAR_LOG_DEBUG);
			$theoriedetails = $result->Details;
			$this->template->assign('theoriedetails',$theoriedetails);
			// TODO: Zwakke punten in Theorie leren WS request
			$zwakkepunten = array();
			if(isset($result->ZwakkePunten)) {
				$zwakkepunten = $result->ZwakkePunten;
			}
			$this->template->assign('zwakkepunten',$zwakkepunten);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				case 1 :
					$errorcode = 24;
					$errormessage = 'Onbekende theorie ID';
					break;
				default :
					$errorcode = 23;
					$errormessage = 'Onbekende fout bij ophalen theorie details ("'.$e->faultstring.'")';
					break;
			}
			$this->errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (TheorieDetailsException $e) {
			$this->errors[] = array (
				'code' => $e->getCode(),
				'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('errors', $this->errors);
	}

	/**
	 * void theorieAanvragenLeerling()
	 * 
	 * Shows the 'Theorie aanvragen' page (for a Leerling that is logged in)
	 * Sets an error array in the template in case of errors.
	 */
	function theorieAanvragenLeerling() {
		$aanvraagresult = false;
		$this->errors = array ();
		try {
			if (!isset ($_POST['theorieType']) || empty ($_POST['theorieType'])) {
				throw new TheorieAanvraagException('Geen theorie type gegeven', 25);
			}
			$result = $this->wsclient->WS_VekaTheorie_Aanvraag(array (
				'RijschoolId' => $this->user->getRijschoolId(),
				'SessionId' => $_SESSION['SessionId'],
				'Type' => $_POST['theorieType']
			));

			$aanvraagresult = $result->Success;
			$this->logger->log('Result from theorie aanvraag ' . print_r($result,'PEAR_LOG_DEFAULT'));
			$this->template->assign('aanvraag_omschrijving',$result->Omschrijving);
			$this->template->assign('aanvraag_prijs',$result->Prijs);
		$this->template->assign('errors', $this->errors);
		} catch (SoapFault $e) {
			$this->logger->log("SOAP Fault: (faultcode: {$e->faultcode} faultstring: {$e->faultstring})", PEAR_LOG_INFO);
			switch ($e->faultcode) {
				default :
					$errorcode = 26;
					$errormessage = 'Onbekende fout bij aanvragen van de theorie ("'.$e->faultstring.'")';
					break;
			}
			$this->errors[] = array (
				'code' => $errorcode,
				'message' => $errormessage
			);
			$this->setSubtitle($errormessage);
		} catch (TheorieAanvraagException $e) {
			$this->errors[] = array (
				'code' => $e->getCode(),
				'message' => $e->getMessage()
			);
			$this->setSubtitle($e->getMessage());
		}
		$this->template->assign('errors', $this->errors);
		return $aanvraagresult;
	}
	
	/**
	 * void waaromRijlesplanner()
	 * 
	 * Shows the 'Waarom Rijlesplanner?' content
	 * Sets an error array in the template in case of errors.
	 */
	function waaromRijlesplanner() {
		$this->errors = array ();
		
		$this->template->assign('errors', $this->errors);
	}

	public function addError($message, $code) {
		$this->errors[] = array (
			'code' => $code,
			'message' => $message
		);
		$this->setSubtitle($message);
	}

	public function getTitle() {
		return $this->title;
	}

	public function getSubTitle() {
		return $this->subtitle;
	}

	public function setSubTitle($newVal = "") {
		if (!empty ($newVal)) {
			$this->subtitle = $newVal;
		}
	}

	/*
	 * string __toString()
	 * 
	 * Returns a String representation of this class.
	 * 
	 * @return string A representation of this class
	 */
	public function __toString() {
		return $this->title;
	}
}
?>
